const express = require("express");
const router = express.Router();
const path = require('path');
const pool = require("../pool.js");
const utils = require("../utils/index.js");
const moment = require("moment-timezone");
const multer = require("multer");

// 配置 multer 的存储设置
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 设置文件上传目录
        cb(null, "/home/wwwroot/default/node-server/files");
    },
    filename: function (req, file, cb) {
        // 生成文件名
        cb(null, Date.now() + "-" + file.originalname);
    },
});

const upload = multer({ storage: storage });

// 创建文件上传的 POST 接口
router.post("/upload", upload.single("file"), (req, res) => {
    console.log(req.headers.host)
    const file = req.file;
    const { pk } = req.body;
    if (!file) {
        return res.status(400).send("请上传一个文件。");
    }
    // 构建文件存储路径
    let fileUrl = ''
    //安卓qrcode 放ins2服务器
    if (pk == 'scan.qr.code.barcode.scanner') {
        fileUrl = `https://fd.freevideodownload.pro/files/${file.filename}`;
    } else {
        fileUrl = `https://insdoss.freeapptools.com/files/${file.filename}`;
    }
    if (pk == "food.scanner.calorie.counter.cal.ai" || pk == "food.calorie.tracker.counter.cal.ai.debug") {
        fileUrl = `https://fd.freevideodownload.pro/files/${file.filename}`;
    }
    if (req.headers.host == 'pdf.seamobitech.com') {
        fileUrl = `https://pdf.seamobitech.com/files/${file.filename}`;

    }
    if (req.headers.host == 'xiaoshuchengzhang.com') {
        fileUrl = `https://xiaoshuchengzhang.com/files/${file.filename}`;

    }
    // 返回文件存储地址
    res.send({
        code: 0,
        message: "文件上传成功",
        fileUrl: fileUrl,
    });
});
module.exports = router;
